package com.example.demo.demos.book.controller;

import com.example.demo.constant.Constants;
import com.example.demo.demos.book.service.UserService;
import com.example.demo.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private  UserService userService;
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){


//        //第一版不结合数据库校验参数
//        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
//            return  false;
//        }
        /*这是第二版结合数据库*/
        UserInfo userInfo=userService.getUserInfoByName(userName);
        if(userInfo==null||userInfo.getId()<=0){
            return false;
        }
        if(password.equals(userInfo.getPassword())){
            userInfo.setPassword("");
            session.setAttribute(Constants.SESSION_USER_KEY,userInfo);
            return true;
        //验证账号密码是否正确
        //为什么admin放在前面，因为假如userName为NULL，这样他就会报一个空指针错误，常量放在前面，开发习惯
        }
        return  false;
    }

    public UserService getUserService() {
        return userService;
    }
}
